package org.lundberg.podcasts;

import org.lundberg.podcasts.R;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.view.MenuItem;

public class PodcastPreferenceActivity extends PreferenceActivity
	implements OnSharedPreferenceChangeListener
{
	public static final String KEEP_OLD_ITEMS_PREF = "keep_old_items";
	public static final String INITIAL_SKIP_PREF = "initial_skip";
	public static final String FWD_SEEK_PREF = "fwd_seek";
	public static final String REV_SEEK_PREF = "rev_seek";
	public static final String UPDATE_FREQ_PREF = "update_frequency";
	public static final String MEDIA_BUTTON_SEEK_PREF = "media_button_seek";
	
	// This is not used by the PreferenceActivity.  It is used internally to keep
	// track of the time the podcasts were last refreshed
	public static final String LAST_UPDATE_PREF = "last_update";
	public static final String NEW_ITEM_COUNT_PREF = "new_item_count";
	
	private ListPreference mKeepItemsPref = null;
	private ListPreference mInitialSkipPref = null;
	private ListPreference mFwdSeekPref = null;
	private ListPreference mRevSeekPref = null;
	private ListPreference mUpdateFreqPref = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);  

        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        addPreferencesFromResource(R.xml.preferences);
    
        // Get a reference to the preferences
        mUpdateFreqPref = (ListPreference) getPreferenceScreen().findPreference(UPDATE_FREQ_PREF);
        mInitialSkipPref = (ListPreference) getPreferenceScreen().findPreference(INITIAL_SKIP_PREF);
        mKeepItemsPref = (ListPreference) getPreferenceScreen().findPreference(KEEP_OLD_ITEMS_PREF);
        mFwdSeekPref = (ListPreference) getPreferenceScreen().findPreference(FWD_SEEK_PREF);
        mRevSeekPref = (ListPreference) getPreferenceScreen().findPreference(REV_SEEK_PREF);
    }
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
		
		// Set up a listener whenever a key changes            
		sharedPreferences.registerOnSharedPreferenceChangeListener(this);
		
		
		String keepOldItems = sharedPreferences.getString(KEEP_OLD_ITEMS_PREF, "5");
        mKeepItemsPref.setSummary("Keep "+keepOldItems+" listened audio files of each podcast");
        
        String updateFrequency = sharedPreferences.getString(UPDATE_FREQ_PREF, "8");
        mUpdateFreqPref.setSummary("every "+updateFrequency+" hours");
        
        String initialSkip = sharedPreferences.getString(INITIAL_SKIP_PREF, "60");
        mInitialSkipPref.setSummary("Skip first "+initialSkip+" seconds of each podcast");

        String revSeek = sharedPreferences.getString(REV_SEEK_PREF, "15");
        mRevSeekPref.setSummary(revSeek+" seconds");
        
        String fwdSeek = sharedPreferences.getString(FWD_SEEK_PREF, "30");
        mFwdSeekPref.setSummary(fwdSeek+" seconds");
	}

	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sp, String key) {

		if (key.equals(INITIAL_SKIP_PREF))
		{
			String initialSkip = sp.getString(INITIAL_SKIP_PREF, "0");
	        mInitialSkipPref.setSummary("Skip first "+initialSkip+" seconds of each podcast");
		}
		else if (key.equals(KEEP_OLD_ITEMS_PREF))
		{
			String keepOldItems = sp.getString(KEEP_OLD_ITEMS_PREF, "5");
	        mKeepItemsPref.setSummary("Keep "+keepOldItems+" listened audio files of each podcast");
		}
		else if (key.equals(REV_SEEK_PREF))
		{
			String revSeek = sp.getString(REV_SEEK_PREF, "15");
	        mRevSeekPref.setSummary(revSeek+" seconds");
		}
		else if (key.equals(FWD_SEEK_PREF))
		{
			String fwdSeek = sp.getString(FWD_SEEK_PREF, "30");
	        mFwdSeekPref.setSummary(fwdSeek+" seconds");
		}
		else if (key.equals(UPDATE_FREQ_PREF))
		{
			String updateFrequency = sp.getString(UPDATE_FREQ_PREF, "8");
	        mUpdateFreqPref.setSummary("every "+updateFrequency+" hours");
		}
	}
	
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; go home
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
